package cn.ssm.petadopt.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Autheor: Sun俊仪
 * @Date: 2022 2022/12/17 16:39
 * @Name: PetSystem
 */
@Controller
@RequestMapping("/capt")
public class CaptchaController {

    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpSession httpSession){
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
        String code = captcha.getCode();
        System.out.println("code = " + code);
        // 获取的验证码数值存入session作用域中
        httpSession.setAttribute("code",code);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            captcha.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
